package com.xiaodou.mjf.web.console.member.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.xiaodou.mjf.application.core.repository.mybatis.AccountMapper;
import com.xiaodou.mjf.application.core.repository.mybatis.InvestMapper;
import com.xiaodou.mjf.application.core.repository.mybatis.MemberMapper;
import com.xiaodou.mjf.application.core.repository.mybatis.NotesBookMapper;
import com.xiaodou.mjf.application.user.service.MemberService;
import com.xiaodou.mjf.entity.core.Account;
import com.xiaodou.mjf.entity.core.Invest;
import com.xiaodou.mjf.entity.core.NotesBook;
import com.xiaodou.mjf.entity.user.Member;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;

/**
 * 会员管理控制器
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("member/user")
public class MemberController extends ApplicationController {
	@Resource
	MemberService memberService;
	@Resource
	NotesBookMapper notesBookMapper;
	@Resource
	InvestMapper investMapper;
	@Resource
	MemberMapper memberMapper;
	@Resource
	AccountMapper accoutMapper;

	private String memberId = "";

	@RequestMapping("")
	public String list(Member member, Model model) {

		return "member/user/list";
	}

	/**
	 * 会员列表
	 * 
	 * @param member
	 * @return
	 */
	@RequestMapping(value = { "data.json" })
	@ResponseBody
	public Map<String, Object> data(Member member) {
		/*
		 * Page<Member> page = memberService.page(member); return page(page);
		 */

		List<Member> entity = Lists.newArrayList();
		if (member.getStart() == 1) {
			member.setStart(0);
		} else {
			member.setStart(member.getStart() - 1);
		}
		Integer count = memberMapper.getMemberAndMobileDetailsCount(member);
		entity = memberMapper.getMemberAndMobileDetails(member);

		return page(count, entity);
	}

	/**
	 * 会员显示
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "view", "view/{id}" })
	public String form(@PathVariable String id, Model model) {
		memberId = id;
		Member member = memberMapper.getmemberDetails(id);
		Invest invest = investMapper.getAssetsTotal(memberId);
		Account account = accoutMapper.getAccountByMemberId(memberId);
		model.addAttribute("account", account);
		model.addAttribute("member", member);
		model.addAttribute("invest", invest);
		return "member/user/overview";
	}

	/**
	 * 交易记录
	 * 
	 * @param notesBook
	 * @return
	 */
	@RequestMapping(value = { "tranData.json" })
	@ResponseBody
	public Map<String, Object> tranData(NotesBook notesBook) {
		List<NotesBook> entity = Lists.newArrayList();
		String id = memberId;
		notesBook.setMemberId(id);
		if (notesBook.getStart() == 1) {
			notesBook.setStart(0);
		} else {
			notesBook.setStart(notesBook.getStart() - 1);
		}
		Integer count = notesBookMapper.getNotesBookCountByMemberId(id);
		entity = notesBookMapper.transactionFlow(notesBook);

		return page(count, entity);
	}

	/**
	 * 购买记录
	 * 
	 * @param notesBook
	 * @return
	 */
	@RequestMapping(value = { "buyRecordData.json" })
	@ResponseBody
	public Map<String, Object> buyRecordData(Invest invest) {
		List<Invest> entity = Lists.newArrayList();
		invest.setMemberId(memberId);

		if (invest.getStart() == 1) {
			invest.setStart(0);
		} else {
			invest.setStart(invest.getStart() - 1);
		}
		Integer count = investMapper.getBuyRecordCount(memberId);
		entity = investMapper.getBuyRecordByMemberId(invest);
		return page(count, entity);
	}

	/**
	 * 所购产品
	 * 
	 * @param notesBook
	 * @return
	 */
	@RequestMapping(value = { "buyProData.json" })
	@ResponseBody
	public Map<String, Object> buyProData(Invest invest) {
		List<Invest> entity = Lists.newArrayList();
		invest.setMemberId(memberId);
		if (invest.getStart() == 1) {
			invest.setStart(0);
		} else {
			invest.setStart(invest.getStart() - 1);
		}
		Integer count = investMapper.getBuyProductCount(memberId);
		entity = investMapper.getBuyProductByMemberId(invest);

		return page(count, entity);
	}

}
